//
//  TeachManagementButtonsInCellScrollView.m
//  Coach_HaiGeEr
//
//  Created by liuchen on 2018/9/27.
//  Copyright © 2018年 海格尔. All rights reserved.
//

#import "TeachManagementButtonsInCellScrollView.h"
@interface TeachManagementButtonsInCellScrollView()
@property (nonatomic,strong) NSMutableArray *tempArr;
@property (nonatomic,strong) NSMutableArray *buttonsArr;
@end
@implementation TeachManagementButtonsInCellScrollView

- (instancetype)initWithFrame:(CGRect)frame WithArray:(NSArray *)subjectTwoSourceArr{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViewWithArray:subjectTwoSourceArr];
    }
    return self;
}

#pragma mark ==========创建视图==========
- (void)createSubViewWithArray:(NSArray *)subjectTwoSourceArr{
    self.tempArr = [NSMutableArray arrayWithArray:subjectTwoSourceArr];
    if (self.tempArr != nil) {
        WS(ws);
        self.buttonsArr = [[NSMutableArray alloc] init];
        self.contentSize = CGSizeMake(125*SCALEUNIT*self.tempArr.count-25*SCALEUNIT, self.frame.size.height);
        self.showsHorizontalScrollIndicator = NO;
        for (int i=0; i<self.tempArr.count; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            btn.tag = 1000+i;
            btn.showsTouchWhenHighlighted = YES;
            [btn setBackgroundColor:[UIColor colorWithHexString:@"#e0e0e0"]];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [btn setTitle:[[self.tempArr objectAtIndex:i] objectForKey:@"itemName"] forState:UIControlStateNormal];
            [btn.titleLabel setNumberOfLines:2];
            [btn.titleLabel setFont:[UIFont systemFontOfSize:LoginFont-3]];
            [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
            [btn addTarget:self action:@selector(selectOpen:) forControlEvents:UIControlEventTouchUpInside];
            UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 95*SCALEUNIT, 95*SCALEUNIT) byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(47.5*SCALEUNIT,47.5*SCALEUNIT)];
            CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
            maskLayer.frame = CGRectMake(0, 0, 95*SCALEUNIT, 95*SCALEUNIT);
            maskLayer.path = maskPath.CGPath;
            btn.layer.mask = maskLayer;
            [self addSubview:btn];
            [self.buttonsArr addObject:btn];
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(ws);
                make.width.height.mas_equalTo(ws.mas_height);
                make.left.mas_equalTo(ws).with.offset(125*SCALEUNIT*i);
            }];
        }
    }
}

#pragma mark ==========数据填充==========
- (void)setTrainItem:(NSString *)trainItem{
    _trainItem = trainItem;
    if ([_trainItem length] != 0) {
        for (int i=0; i<self.tempArr.count; i++) {
            if ([_trainItem containsString:[NSString stringWithFormat:@"%@",[[self.tempArr objectAtIndex:i] objectForKey:@"id"]]]) {
                UIButton *btn = [self.buttonsArr objectAtIndex:i];
                [btn setBackgroundColor:[UIColor colorWithHexString:@"#676fe9"]];
            } else{
                UIButton *btn = [self.buttonsArr objectAtIndex:i];
                [btn setBackgroundColor:[UIColor colorWithHexString:@"#e0e0e0"]];
            }
        }
    } else {
        for (int i=0; i<self.tempArr.count; i++) {
            UIButton *btn = [self.buttonsArr objectAtIndex:i];
            [btn setBackgroundColor:[UIColor colorWithHexString:@"#e0e0e0"]];
        }
    }
}

#pragma mark ==========按钮点击==========
- (void)selectOpen:(UIButton *)btn{
    btn.userInteractionEnabled = NO;
    if ([self.buttonsDelegate respondsToSelector:@selector(teachManagementButtonsInCellScrollViewDelegateWithTag:WithID:WithitemId:WithProjectName:WithStudentName:)]) {
        [self.buttonsDelegate teachManagementButtonsInCellScrollViewDelegateWithTag:btn WithID:self.ID WithitemId:[NSString stringWithFormat:@"%@",[[self.tempArr objectAtIndex:btn.tag-1000] objectForKey:@"id"]] WithProjectName:[[self.tempArr objectAtIndex:btn.tag-1000] objectForKey:@"itemName"] WithStudentName:self.studentName];
    }
}

@end
